﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pcalc
{
	class CommandList
	{
		List<string> _commands = new List<string>();
		int _maxCount;
		int _index;

		public CommandList()
		{
			_maxCount = 20;
			_index = -1;
		}

		public void AddCommand(string cmd)
		{
			if (cmd.Length > 0)
			{
				if (_commands.Contains(cmd))
				{
					_commands.Remove(cmd);
				}

				_commands.Add(cmd);

				if (_commands.Count > _maxCount)
				{
					_commands.RemoveAt(0);
				}
			}

			ResetIndex();
		}

		public string GetPrevCommand()
		{
			if (_commands.Count == 0)
				return null;

			if (_index == -1)
				_index = _commands.Count - 1;
			else if (_index != 0)
				_index--;

			return _commands[_index];
		}

		public string GetNextCommand()
		{
			if (_commands.Count == 0)
				return null;

			if (_index == -1)
				return null;
			
			if (_index == _commands.Count - 1)
			{
				_index = -1;
				return null;
			}

			return _commands[++_index];
		}

		public void ResetIndex()
		{
			_index = -1;
		}
	}
}
